package com.lovo.sqlatm;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Revise extends JPanel implements ActionListener {
    private JTextField oldPassword =new JTextField();
    private JTextField newldPassword =new JTextField();
    private JTextField confirmPass =new JTextField();
    private Test0 frame;

    private Icon conImage = Util.getIcon("/img/buttonSure.jpg");
    private Icon cancelImage = Util.getIcon("/img/buttonOff.jpg");
    private JButton confirmButton;
    private JButton cancelButton;
   public Revise(Test0 frame){
       this.frame=frame;
       this.setLayout(null);
       JLabel oldPasswordText = new JLabel("请输入你的旧密码：");
       oldPasswordText.setBounds(314, 338, 160, 40);
       this.add(oldPasswordText);

       JLabel newldPasswordText = new JLabel("请输入你的新密码：");
       newldPasswordText.setBounds(314, 400, 160, 40);
       this.add(newldPasswordText);

       JLabel ConfirmPassText = new JLabel("确认密码：");
       ConfirmPassText.setBounds(314, 460, 160, 40);
       this.add(ConfirmPassText);

//旧密码
       oldPassword.setBounds(454,338,160,40);
       this.add(oldPassword);
       oldPassword.getText();

       //新密码
       newldPassword.setBounds(454,400,160,40);
       this.add(newldPassword);
       newldPassword.getText();

       //确认新密码
       confirmPass.setBounds(454,460,160,40);
       this.add(confirmPass);
       confirmPass.getText();



       //确认按钮
       confirmButton=new JButton(conImage);
       confirmButton.setBounds(354,500,140,50);
       confirmButton.addActionListener(this);
       this.add(confirmButton);
       //返回按钮
       cancelButton=new JButton(cancelImage);
       cancelButton.setBounds(540,500,140,50);
       this.add(cancelButton);
       cancelButton.addActionListener(this);

       this.setOpaque(false);

   }

    @Override
    public void actionPerformed(ActionEvent e) {
       if (e.getSource()==confirmButton){
           String olbpas=oldPassword.getText();
           String newpas=newldPassword.getText();
           String confirmPas=confirmPass.getText();
           JdbcUtil jdbcUtil =new JdbcUtil();
           Uesr uesr= jdbcUtil.selectId(frame.getUesr().getId());

           if (olbpas.equals(uesr.getPassword())){
               if (newpas.equals(confirmPas)){
                   uesr.setPassword(confirmPas);
                   jdbcUtil.update(uesr);

                   JOptionPane.showMessageDialog(this,"修改密码成功");
               }else {
                   JOptionPane.showMessageDialog(this,"两次密码错误");
               }
           }else {
               JOptionPane.showMessageDialog(this,"旧密码输入错误");
           }

       }else if (e.getSource()==cancelButton){
           frame.switchPanel("menu");

       }

    }
}
